//
// Created by benxb on 2021/12/29.
//

#ifndef BENXDB_TIMER_H
#define BENXDB_TIMER_H

#include <chrono>
#include <common/logger.h>
#include <string>

namespace benxdb {

class Timer {
public:
    Timer() : duration_(std::chrono::steady_clock::duration::zero()) {}

    ~Timer() = default;

    void Start() {
        start_ = std::chrono::steady_clock::now();
    }

    void Resume() {
        start_ = std::chrono::steady_clock::now();
    }

    void Pause() {
        duration_ += std::chrono::steady_clock::now() - start_;
    }

    void Clear() {
        duration_ = std::chrono::steady_clock::duration::zero();
    }

    void StopAndPrint(const std::string& msg) {
        duration_ += std::chrono::steady_clock::now() - start_;
        LOG_DEBUG("%s cost: %f", msg.c_str(), duration_.count());
        duration_ = std::chrono::steady_clock::duration::zero();
    }

    void PauseAndPrint(const std::string& msg) {
        std::chrono::duration<double> diff = std::chrono::steady_clock::now() - start_;
        duration_ += diff;
        LOG_DEBUG("%s cost: %f", msg.c_str(), diff.count());
    }

private:
    std::chrono::time_point<std::chrono::steady_clock> start_;
    std::chrono::duration<double> duration_;
};

}

#endif //BENXDB_TIMER_H
